package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Arrays;

public class StringTest extends JFrame {

	private JPanel contentPane;
	private JLabel lblOriginalText;
	private JTextField str;
	private JLabel lblResult;
	private JScrollPane scrollPane;
	private JTextArea result;
	private JLabel lblNewLabel;
	private JTextField test;
	private JButton btnCharAt;
	private JButton btnContains;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JButton btnLastindexof;
	private JButton btnLength;
	private JButton btnReplace;
	private JButton btnReplaceall;
	private JButton btnSplit;
	private JButton btnSubstring;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					StringTest frame = new StringTest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public StringTest() {
		setTitle("String Test");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 591, 505);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getStr());
		contentPane.add(getLblResult());
		contentPane.add(getLblOriginalText());
		contentPane.add(getScrollPane());
		contentPane.add(getLblNewLabel());
		contentPane.add(getTest());
		contentPane.add(getBtnCharAt());
		contentPane.add(getBtnContains());
		contentPane.add(getBtnNewButton());
		contentPane.add(getBtnNewButton_1());
		contentPane.add(getBtnLastindexof());
		contentPane.add(getBtnLength());
		contentPane.add(getBtnReplace());
		contentPane.add(getBtnReplaceall());
		contentPane.add(getBtnSplit());
		contentPane.add(getBtnSubstring());
	}

	private JLabel getLblOriginalText() {
		if (lblOriginalText == null) {
			lblOriginalText = new JLabel("Original String");
			lblOriginalText.setHorizontalAlignment(SwingConstants.RIGHT);
			lblOriginalText.setBounds(12, 10, 85, 15);
		}
		return lblOriginalText;
	}
	private JTextField getStr() {
		if (str == null) {
			str = new JTextField();
			str.setBounds(109, 7, 454, 21);
			str.setColumns(10);
		}
		return str;
	}
	private JLabel getLblResult() {
		if (lblResult == null) {
			lblResult = new JLabel("Result");
			lblResult.setBounds(22, 132, 57, 15);
		}
		return lblResult;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 157, 553, 300);
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}
	private JTextArea getResult() {
		if (result == null) {
			result = new JTextArea();
		}
		return result;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("Test String");
			lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel.setBounds(12, 41, 85, 15);
		}
		return lblNewLabel;
	}
	private JTextField getTest() {
		if (test == null) {
			test = new JTextField();
			test.setBounds(109, 38, 454, 21);
			test.setColumns(10);
		}
		return test;
	}
	private JButton getBtnCharAt() {
		if (btnCharAt == null) {
			btnCharAt = new JButton("charAt");
			btnCharAt.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						int pos = Integer.parseInt(test.getText());
						char c = str.getText().charAt(pos);
						result.append("\ncharAt at "+pos+"th of Original String : " +c+"\n");
						
					}catch(Exception ex){
						result.setText("Error!\n1.Please put a number at 'Test String' field. \n 2.Your Number is bigger than Original String's length.");
					}
					
				}
			});
			btnCharAt.setBounds(12, 66, 97, 23);
		}
		return btnCharAt;
	}
	private JButton getBtnContains() {
		if (btnContains == null) {
			btnContains = new JButton("Contains");
			btnContains.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					try{
					if(str.getText().contains(test.getText())){  
						result.append("\n"+test.getText()+" is included in your Original String\n");
					}else{
						result.append("\n"+test.getText()+" isn't included in your Original String\n");
					}
					}catch(Exception ex){
						result.append("\nCheck your Original String");
					}
				}
			});
			btnContains.setBounds(119, 66, 97, 23);
		}
		return btnContains;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("IgnoreCase");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						if(str.getText().equalsIgnoreCase(test.getText())){
							result.append("\nSame");
						}else{
							result.append("\nDifferent");
						}
					}catch(Exception ex){
						
					}
				}
			});
			btnNewButton.setBounds(228, 66, 97, 23);
		}
		return btnNewButton;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("IndexOf");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						int a = str.getText().indexOf(test.getText());
						if(a==-1){
							result.append("\nThere is not exist");
						}else{
							result.append("\n"+test.getText() + " is " + (a+1) + ".th in your Original String.");
						}
					}catch(Exception ex){
						
					}
				}
			});
			btnNewButton_1.setBounds(337, 66, 97, 23);
		}
		return btnNewButton_1;
	}
	private JButton getBtnLastindexof() {
		if (btnLastindexof == null) {
			btnLastindexof = new JButton("lastIndexOf");
			btnLastindexof.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						int a = str.getText().lastIndexOf(test.getText());
						if(a==-1){
							result.append("\nThere is not exist");
						}else{
							result.append("\n"+test.getText() + " is " + a + ".th in your Original String.");
						}
					}catch(Exception ex){
						
					}
				}
			});
			btnLastindexof.setBounds(446, 66, 97, 23);
		}
		return btnLastindexof;
	}
	private JButton getBtnLength() {
		if (btnLength == null) {
			btnLength = new JButton("length");
			btnLength.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						int a = str.getText().length();
						result.append("\nYour Original String's Length is " + a);
					}catch(Exception ex){
						result.append("\nPlease fill test form");
					}
				}
			});
			btnLength.setBounds(12, 99, 97, 23);
		}
		return btnLength;
	}
	private JButton getBtnReplace() {
		if (btnReplace == null) {
			btnReplace = new JButton("replace");
			btnReplace.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{						
						result.append("\n"+str.getText().replace("a", test.getText()));
					}catch(Exception ex){
						result.append("\nPlease fill both fields");
					}
				}
			});
			btnReplace.setBounds(121, 99, 97, 23);
		}
		return btnReplace;
	}
	private JButton getBtnReplaceall() {
		if (btnReplaceall == null) {
			btnReplaceall = new JButton("replaceAll");
			btnReplaceall.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						result.append("\n"+str.getText().replaceAll(str.getText(), test.getText()));
					}catch(Exception ex){
						result.append("\nPlease fill both fields");
					}
				}
			});
			btnReplaceall.setBounds(230, 99, 97, 23);
		}
		return btnReplaceall;
	}
	private JButton getBtnSplit() {
		if (btnSplit == null) {
			btnSplit = new JButton("split");
			btnSplit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						String[] sp = str.getText().split(test.getText());
						result.append("\n"+Arrays.toString(sp));
					}catch(Exception ex){
						/*result.append("\nPlease fill both fields");*/
					}
				}
			});
			btnSplit.setBounds(337, 99, 97, 23);
		}
		return btnSplit;
	}
	private JButton getBtnSubstring() {
		if (btnSubstring == null) {
			btnSubstring = new JButton("substring");
			btnSubstring.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						String temp = str.getText();
						String r = temp.substring(2,21);
						result.append("\nSubstring r : " + r);
					}catch(Exception ex){
						
					}
				}
			});
			btnSubstring.setBounds(446, 99, 97, 23);
		}
		return btnSubstring;
	}
}
